package com.my.demo.controller.enterprise;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.pojo.EnterpriseUser;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.User;
import com.my.demo.service.EnterpriseSpaceService;
import com.my.demo.service.EnterpriseUserService;
import com.my.demo.service.PermissonService;
import com.my.demo.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GetEnterpriseUserController {
    @Autowired
    EnterpriseUserService enterpriseUserService;
    @Autowired
    EnterpriseSpaceService enterpriseSpaceService;
    @Autowired
    UserService userService;
    @Autowired
    PermissonService permissonService;

    @RequiresRoles("administrator")
    @PostMapping("/get_enterprise_user")
    @CrossOrigin
    public Result getEnterpriseUser(@RequestBody Map<String,String> map){
        Result result = new Result<>();
        result.setSuccess(false);
        result.setMsg(null);

        //搜索范围
        String scope = map.get("scope");
        QueryWrapper<EnterpriseUser> enterpriseUserQueryWrapper=new QueryWrapper<>();
        if (scope.equals("1")){
            enterpriseUserQueryWrapper.eq("type",1);
        }
        else if (scope.equals("2")){
            enterpriseUserQueryWrapper.eq("type",2);
        }
        else if (scope.equals("3")){
            enterpriseUserQueryWrapper.eq("type",3);
        }


        Integer spaceId=0;
        try {
            String userName = (String) SecurityUtils.getSubject().getPrincipal();
            Integer userId= userService.selectIdByName(userName);
            spaceId = permissonService.getById(userId).getSpaceNo();
        }catch (Exception exception){
            exception.printStackTrace();
        }

        if (spaceId!=null&&spaceId>0){
            enterpriseUserQueryWrapper.eq("space_no",spaceId);
        }
        else {
            System.out.println("获取spaceID出错");
            return result;
        }

        System.out.println("当前的空间ID："+spaceId);

        List<EnterpriseUser> enterpriseUserList = null;
        try {
            enterpriseUserList=enterpriseUserService.list(enterpriseUserQueryWrapper);
        }catch (Exception exception){
            exception.printStackTrace();
        }
        if (enterpriseUserList==null||enterpriseUserList.isEmpty()){
            return result;
        }
        //获取该企业空间中所有用户的id,phone,name
        List<Map> resultList = new ArrayList<>();
        Integer userId=0;
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        String searchUserName = map.get("searchUserName");
        for (EnterpriseUser enterpriseUser : enterpriseUserList) {
            Map<String,Object> medMap=new HashMap<>();
            userId=enterpriseUser.getUserNo();
            System.out.println("用户ID："+userId);
            userQueryWrapper.eq("userId",userId);
            if (searchUserName!=null&&!searchUserName.equals("")){
                userQueryWrapper.like("user_name",searchUserName);
            }
            User user=null;
            try {
                user=userService.getOne(userQueryWrapper);
            }catch (Exception exception){
                exception.printStackTrace();
            }
            //搜索的用户不匹配
            if (user==null){
                userQueryWrapper.clear();
                continue;
            }
            medMap.put("userName",user.getUserName());
            medMap.put("phone",user.getUserName());
            switch (enterpriseUser.getType()){
                case 1:
                    medMap.put("type","创建者");
                    break;
                case 2:
                    medMap.put("type","管理员");
                    break;
                case 3:
                    medMap.put("type","企业成员");
                    break;
            }
            resultList.add(medMap);
            userQueryWrapper.clear();
        }

        if (resultList==null||resultList.isEmpty()){
            return result;
        }

        result.setSuccess(true);
        result.setMsg("查询企业成员成功");
        result.setDetail(resultList);
        return result;
    }

}
